package com.author.surface;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.font.FontRenderContext;
import java.awt.font.GlyphVector;

import javax.swing.JToggleButton;
import javax.swing.KeyStroke;
import javax.swing.border.BevelBorder;
import javax.swing.text.StyledEditorKit;

public class UnderlineAction extends JToggleButton {
	/** Width and height of the BOLD action button */
	private static final int	UNDERLINE_BUTTON_WIDTH	= 30;
	private static final int	UNDERLINE_BUTTON_HEIGHT	= 30;

	/** Font used to display the "B" letter inside the BOLD action button */
	private static final Font	underlineButtonFont		= new Font(
																"Courier New",
																Font.BOLD, 24);

	/**
	 * X and Y co-ordinates which will be used to position the "U" letter inside
	 * the UNDERLINE action button
	 */
	private static final int	TX						= 8;
	private static final int	TY						= 23;

	/** characters that are used for display inside the UNDERLINE button */
	public static final String	UNDERLINE				= "U";

	/** Underline action for the Styled Editor kit */
	private static class underlineAction extends
			StyledEditorKit.UnderlineAction {
		public underlineAction() {
			super();
			putValue(NAME, ""); // name is not used
			putValue(ACCELERATOR_KEY,
					KeyStroke.getKeyStroke(KeyEvent.VK_U, InputEvent.CTRL_MASK));
		}

		@Override
		public void actionPerformed(ActionEvent event) {
			super.actionPerformed(event);
		}
	};

	/**
	 * Underline action when invoked by the user will set the characters in the
	 * pane bold. This will usually be invoked through the CTRL+U combination of
	 * keystrokes
	 */
	private static underlineAction	underline_action	= new underlineAction();

	public UnderlineAction() {
		// Add underline action to the Toggle Button
		super(underline_action);

		// Set size of the UNDERLINE button
		setSize(new Dimension(UNDERLINE_BUTTON_WIDTH,
				UNDERLINE_BUTTON_HEIGHT + 1));
		setPreferredSize(getSize());
		setEnabled(true);

		// Set border of the UNDERLINE button
		setBorder(new BevelBorder(BevelBorder.LOWERED));
		setBorderPainted(false);

		// Set visible properties
		setToolTipText("Underline");
		setFocusable(false);
		setVisible(true);

		// Make the button invoke the action when a specific sequence of
		// keystrokes is fired by the user
		getInputMap(WHEN_IN_FOCUSED_WINDOW).put(
				KeyStroke.getKeyStroke(KeyEvent.VK_U, InputEvent.CTRL_MASK),
				"ctrl u");
		getActionMap().put("ctrl u", underline_action);
	}

	/**
	 * This method will be executed when the UI updater requests that the button
	 * responsible for displaying Underline font is either selected or
	 * de-selected
	 */
	public void setUnderline(boolean value) {
		if (value == true) {
			if (!isSelected()) {
				setSelected(true);
			}
		}

		if (value == false) {
			if (isSelected()) {
				setSelected(false);
			}
		}
	}

	public void paint(Graphics g) {
		super.paint(g);

		Graphics2D g2d = (Graphics2D) g;
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);
		FontRenderContext frc = g2d.getFontRenderContext();
		GlyphVector glyphs = underlineButtonFont.createGlyphVector(frc,
				UNDERLINE);

		g2d.drawGlyphVector(glyphs, TX, TY + 1);
	}
}
